<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<div class="span8 offset2">
    <form class="form-horizontal well" method="POST" action="MainServlet?action=route.save" onsubmit="return checkRoute();">
        <fieldset>
            <legend>Edit route</legend>            
            <div class="hidden">
                <input type="hidden" name="id" value="<c:out value="${route.getId()}" />" >
            </div>         

            <div id="routeNumberControlGroup" class="control-group">
                <label class="control-label" for="routeNumber">Route number:</label>
                <div class="controls">
                    <input class="myinput" type="text" id="routeNumber" name="routeNumber" value="<c:out value="${route.getRouteNumber()}" />" >
                    <span id="routeNumberHelpInline" class="help-inline"></span>
                </div>
            </div>

            <div id="routeNameControlGroup" class="control-group">
                <label class="control-label" for="routeName">Route name:</label>
                <div class="controls">
                    <input class="myinput" type="text" id="routeName" name="routeName" value="<c:out value="${route.getRouteName()}" />" >
                    <span id="routeNameHelpInline" class="help-inline"></span>
                </div>
            </div>

            <div id="startPointControlGroup" class="control-group">
                <label class="control-label" for="startPoint">Start point:</label>
                <div class="controls">
                    <select class="myinput" id="startPoint" name="startPoint">
                        <c:forEach var="startStoppingPoint" items="${stoppingPointList}">
                            <c:choose>                                   
                                <c:when test="${route.getStartPoint().getStoppingPointName() != startStoppingPoint.getStoppingPointName()}">
                                    <option value="<c:out value="${startStoppingPoint.getId()}" />"><c:out value="${startStoppingPoint.getStoppingPointName()}" /></option>
                                </c:when>
                                <c:when test="${route.getStartPoint().getStoppingPointName() == startStoppingPoint.getStoppingPointName()}">
                                    <option selected="true" value="<c:out value="${startStoppingPoint.getId()}" />"><c:out value="${startStoppingPoint.getStoppingPointName()}" /></option>
                                </c:when>
                            </c:choose>
                        </c:forEach>
                    </select>
                    <span id="startPointHelpInline" class="help-inline"></span>
                </div>
            </div>

            <div id="endPointControlGroup" class="control-group">
                <label class="control-label" for="endPoint">End point:</label>
                <div class="controls">
                    <select class="myinput" id="endPoint" name="endPoint">
                        <c:forEach var="endStoppingPoint" items="${stoppingPointList}">
                            <c:choose>                                   
                                <c:when test="${route.getEndPoint().getStoppingPointName() != endStoppingPoint.getStoppingPointName()}">
                                    <option value="<c:out value="${endStoppingPoint.getId()}" />"><c:out value="${endStoppingPoint.getStoppingPointName()}" /></option>
                                </c:when>
                                <c:when test="${route.getEndPoint().getStoppingPointName() == endStoppingPoint.getStoppingPointName()}">
                                    <option selected="true" value="<c:out value="${endStoppingPoint.getId()}" />"><c:out value="${endStoppingPoint.getStoppingPointName()}" /></option>
                                </c:when>                                   
                            </c:choose>
                        </c:forEach>
                    </select>
                    <span id="endPointHelpInline" class="help-inline"></span>
                </div>
            </div>

            <div id="intermediateStoppingPointsListControlGroup" class="control-group">
                <label class="control-label" for="intermediateStoppingPointsList">Intermediate stopping points:</label>
                <div class="controls">
                    <fieldset name="intermediateStoppingPointsList" id="intermediateStoppingPointsList">
                        <div style="width: 220px; height: 200px; overflow: auto">
                            <c:forEach var="intermediateStoppingPoint" items="${stoppingPointList}">
                                <label class="checkbox">
                                    <c:choose>
                                        <c:when test="${route.getIntermediateStoppingPointsList().contains(intermediateStoppingPoint)}">
                                            <input type="checkbox" name="intermediateStoppingPointsList" checked="true" id="intermediateStoppingPointsList_<c:out value="${intermediateStoppingPoint.getId()}" />" value="<c:out value="${intermediateStoppingPoint.getId()}" />" /><c:out value="${intermediateStoppingPoint.getStoppingPointName()}" />
                                        </c:when>
                                        <c:when test="${!route.getIntermediateStoppingPointsList().contains(intermediateStoppingPoint)}">
                                            <input type="checkbox" name="intermediateStoppingPointsList" id="intermediateStoppingPointsList_<c:out value="${intermediateStoppingPoint.getId()}" />" value="<c:out value="${intermediateStoppingPoint.getId()}" />" /><c:out value="${intermediateStoppingPoint.getStoppingPointName()}" />
                                        </c:when>
                                    </c:choose>        
                                </label>
                            </c:forEach>
                        </div>
                    </fieldset>    
                    <span id="intermediateStoppingPointsListHelpInline" class="help-inline"></span>
                </div>
            </div>

            <div id="routeTypeControlGroup" class="control-group">
                <label class="control-label" for="routeType">Route type:</label>
                <div class="controls">
                    <select class="myinput" id="routeType" name="routeType">
                        <c:forEach var="routeType" items="${routeTypeList}">
                            <c:choose>
                                <c:when test="${routeType != route.getRouteType()}">
                                    <option value="<c:out value="${routeType.toString()}" />"><c:out value="${routeType.toString()}" /></option>
                                </c:when>
                                <c:when test="${routeType == route.getRouteType()}">
                                    <option selected="true" value="<c:out value="${routeType.toString()}" />"><c:out value="${routeType.toString()}" /></option>
                                </c:when>
                            </c:choose>
                        </c:forEach>
                    </select>
                    <span id="routeTypeHelpInline" class="help-inline"></span>
                </div>
            </div>

            <div class="form-actions">
                <input type="submit" class="btn btn-primary" name="saveButton" value="Save route data">
                <button class="btn" id="cancelButton" name="cancelButton" data-button-to-click="cancelButtonOutOfForm">Cancel</button>
            </div>

        </fieldset>
    </form>

    <button class="btn" id="cancelButtonOutOfForm" data-link="route-list" style="display: none; visibility: hidden; height: 0px; width: 0px;">Cancel</button>
</div>